# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     test
   Description :
   Author :       Flyoung
   date：          2023/9/4
-------------------------------------------------
   Change Activity:
                   2023/9/4:
-------------------------------------------------
"""
import pandas as pd
import numpy as np
from db.model.city import add_city, get_all_city
from db.model.education import add_education, get_all_education
from db.model.job import Job, add_job
from db.model.job_education import add_job_education
from db.model.job_tech import add_job_tech
from db.model.job_welfare import add_job_welfare
from db.model.tech import add_tech, get_all_tech
from db.model.welfare import add_all_welfare, get_all_welfare
from tqdm import tqdm
import threading
import datetime


def now_time_str():
    # 获取当前时间
    current_time = datetime.datetime.now()
    # 将当前时间格式化为字符串
    return current_time.strftime("%Y-%m-%d %H:%M:%S")


def remove_blank(s: str) -> str:
    try:
        if s is None:
            return ''
        return s.replace(' ', '').replace('\n', '').replace('\t', '')
    except:
        return ''


def get_city_dict():
    all_city = get_all_city()
    city_dict = dict()
    for city in all_city:
        city_dict[city['city_name']] = city['city_id']
    return city_dict


def get_welfare_dict():
    all_welfare = get_all_welfare()
    welfare_dict = dict()
    for welfare in all_welfare:
        welfare_dict[welfare['welfare_name']] = welfare['welfare_id']
    return welfare_dict


def get_education_dict():
    all_educations = get_all_education()
    education_dict = {}
    for education in all_educations:
        education_dict[education['education_name']] = education['education_id']
    return education_dict


def get_tech_dict():
    all_techs = get_all_tech()
    tech_dict = {}
    for tech in all_techs:
        tech_dict[tech['tech_name']] = tech['tech_id']
    return tech_dict


def add_tech_table(data_frame):
    tech_names = data_frame['tech_name']
    tech_name_list = set()
    for techs in tech_names:
        try:
            for tech in remove_blank(techs).split('|'):
                tech_name_list.add(tech)
        except:
            pass
    for tech in tech_name_list:
        add_tech(tech_name=tech)


def add_city_table(df):
    city_names = list(set(df['city_name']))
    for city in tqdm(city_names):
        add_city(city_name=city)


def add_welfare_table(df):
    welfare_names = df['welfare']
    welfare_list = set()
    for welfare_name in welfare_names:
        for welfare in remove_blank(welfare_name).split('，'):
            welfare_list.add(welfare)
    # print(len(welfare_list))
    add_all_welfare(welfare_names=list(welfare_list))


def add_education_table(df):
    education_names = df['education_name']
    education_name_list = set()
    for education_name in education_names:
        education_name_list.add(remove_blank(education_name))
    for i in tqdm(education_name_list):
        add_education(education_name=i)


def split_dataframe(data_frame, n):
    # 将 DataFrame 转换为 numpy 数组
    array = data_frame.to_numpy()

    # 使用 np.array_split() 函数将数组分割成 n 个部分
    split_arrays = np.array_split(array, n)

    # 将每个分割后的数组转换回 DataFrame
    split_dfs = [pd.DataFrame(arr, columns=data_frame.columns) for arr in split_arrays]

    return split_dfs


def add_job_table(data_frame, name):
    data = data_frame.to_dict(orient='records')
    tech_d = get_tech_dict()
    welfare_d = get_welfare_dict()
    city_d = get_city_dict()
    education_d = get_education_dict()
    # 打印每一行数据
    data_tqdm = tqdm(data)
    for row in data_tqdm:
        data_tqdm.set_description(f'[{now_time_str()}] thread {name} ==>')
        job_name = row['job_name']
        company = row['Company']
        education_id = education_d[row['education_name']]
        tech_ids = list()
        welfare_ids = list()
        try:
            for tech in remove_blank(row['tech_name']).split('|'):
                if tech in tech_d:
                    tech_ids.append(tech_d[tech])
        except:
            print(f"tech error ==> {row['tech_name']}")
        try:
            for welfare in remove_blank(row['welfare']).split('，'):
                if welfare in welfare_d:
                    welfare_ids.append(welfare_d[welfare])
        except:
            print(f"welfare error ==> {row['welfare']}")
        city_id = city_d[row['city_name']]

        try:
            salary_lower_limit = row['salary_lower_limit']
            salary_upper_limit = row['salary_upper_limit']
            experience_lower_limit = row['experience_lower_limit']
            experience_upper_limit = row['experience_upper_limit']

            salary_lower_limit = salary_lower_limit[:salary_lower_limit.index('k')]
            if 'K' in salary_upper_limit:
                salary_upper_limit = salary_upper_limit[:salary_upper_limit.index('K')]
            elif '元' in salary_upper_limit:
                salary_upper_limit = float(salary_upper_limit[:salary_upper_limit.index('元')]) / 1000

            experience_lower_limit = experience_lower_limit[:experience_lower_limit.index('年')]
            experience_upper_limit = experience_upper_limit[:experience_upper_limit.index('年')]

            company_size_lower_limit = row['company_size_lower']
            company_size_upper_limit = row['company_size_upper']

            float(company_size_upper_limit)
            float(company_size_lower_limit)
        except Exception as e:
            print(e)
            print(job_name)

        job = Job(
            job_name=job_name,
            city_id=city_id,
            salary_upper_limit=salary_upper_limit,
            salary_lower_limit=salary_lower_limit,
            experience_upper_limit=experience_upper_limit,
            experience_lower_limit=experience_lower_limit,
            company=company,
            company_size_upper_limit=company_size_upper_limit,
            company_size_lower_limit=company_size_lower_limit,
            job_score=0
        )
        job = add_job(job)
        job_id = job['job_id']
        for tech_id in tech_ids:
            add_job_tech(job_id=job_id, tech_id=tech_id)
        for welfare_id in welfare_ids:
            add_job_welfare(job_id=job_id, welfare_id=welfare_id)
        add_job_education(job_id=job_id, education_id=education_id)


def import_all_data(data_frame, n_thread):
    split_dfs = split_dataframe(data_frame, n_thread)
    for i, data_frame_ in enumerate(split_dfs):
        thread = threading.Thread(target=add_job_table, kwargs={
            'data_frame': data_frame_,
            'name': i
        })
        thread.start()


if __name__ == '__main__':
    df = pd.read_csv('data/new_job_info.csv', encoding='utf8')
    import_all_data(df, 12)
